<?php
/**
 * User: Netman
 * Date: 14-3-27
 * Time: 下午9:53
 */

class Stourweb_Controller extends Class_Controller {

    /*
     * before
     */
    public function before()
    {
        parent::before();
        $params = $this->request->param('params');
        $this->params = $this->analyze_param($params);
        $this->assign('admindir', Common::get_sys_para('cfg_admin_dirname'));
    }
    /**
     * 登录检测
     */
    protected function validate_login()
    {
        //未设置默认有效期为2h
        $hours = intval(Common::get_sys_para('cfg_admin_login_time_limit'))>0 ? intval(Common::get_sys_para('cfg_admin_login_time_limit')) : 2;
        $cookie_username = Cookie::get('username');
        if($this->request->is_ajax() && Common::checkLogin($cookie_username))
            return;

        $session = Session::instance();
        $session_username = $session->get('username');
        $uploadcookie = Arr::get($_POST,'uploadcookie');

        $serectkey = !empty($cookie_username) ? $cookie_username : $session_username;
        $serectkey = !empty($serectkey) ? $serectkey : $uploadcookie;
        if (isset($serectkey))
        {
            $lifetime = Cookie::get('lifetime');
            if(time()>intval($lifetime))
            {
                $session = Session::instance();
                $session->delete('username');
                $session->delete('userid');
                $session->delete('roleid');
                Cookie::delete('lifetime');
                Cookie::delete('username');
                header("location:/" . Common::get_sys_para('cfg_admin_dirname'));
                exit;
            }
            $result = Common::checkLogin($serectkey);
            if(!$result)
            {
                $session = Session::instance();
                $session->delete('username');
                $session->delete('userid');
                $session->delete('roleid');
                Cookie::delete('lifetime');
                Cookie::delete('username');
                header("location:/" . Common::get_sys_para('cfg_admin_dirname'));
                exit;
            }
            else
            {
                $session = Session::instance();
                $serectkey = Common::authcode($result['username'].'||'.$result['password'],'');
                $session->set('username',$serectkey);
                $session->set('userid',$result['id']);
                $session->set('roleid',$result['roleid']);
                Cookie::set('lifetime',time()+$hours * 3600,$hours * 3600);
                Cookie::set('username', $serectkey,$hours * 3600);
                $rolemodule=ORM::factory('role_module')->where("roleid='{$result[0]['roleid']}'")->as_array();
                $session->set('rolemodule',$rolemodule);
            }

        }
        else
        {
            $session = Session::instance();
            $session->delete('username');
            $session->delete('userid');
            $session->delete('roleid');
            Cookie::delete('lifetime');
            Cookie::delete('username');
            header("location:/" . Common::get_sys_para('cfg_admin_dirname'));
            exit;
        }
    }
}
